package com.shujia.mr.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 如果需要在集群中执行，那么对应输入输出的路径需要做修改，修改成HDFS中的路径
 *  执行命令为：
 *      hadoop jar hadoopCode-1.0.jar com.shujia.mr.wordcount.MyCombineDriver
 */
public class MyCombinerDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();

        // 一个MapReduce程序就是一个Job
        Job job = Job.getInstance(conf);
        job.setJarByClass(MyCombinerDriver.class);
        job.setJobName("combiner");

        // 设置Map和Reduce过程的处理类
        job.setMapperClass(MyCombinerMapper.class);
        job.setReducerClass(MyCombinerReducer.class);

        // 设置Map阶段输出的Key和Value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 设置最终输出的Key和Value的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        /**
         * Map-Reduce Framework
         * 		Map input records=1098250
         * 		Map output records=2280970
         * 		Map output bytes=25597490
         * 		Map output materialized bytes=232
         * 		Input split bytes=508
         * 		Combine input records=2280970 <- Combine输入为Map函数输出的条数
         * 		Combine output records=16  <- 一共有4个MapTask 每个MapTask对应一个Combine输出每个 Combine输出对应4条
         * 		Reduce input groups=4
         * 		Reduce shuffle bytes=232
         * 		Reduce input records=16  <- Reduce输入为Combine输出的条数
         * 		Reduce output records=4
         * 		Spilled Records=32
         * 		Shuffled Maps =4
         * 		Failed Shuffles=0
         * 		Merged Map outputs=4
         * 		GC time elapsed (ms)=196
         */
        job.setCombinerClass(MyCombinerReducer.class);


        // 设置输入输出的路径
        FileInputFormat.addInputPath(job,new Path("data/combine"));
        FileOutputFormat.setOutputPath(job,new Path("output/combiner"));


        // 提交Job并执行
        job.waitForCompletion(true);

    }
}
